package cn.getech.data.development.utils.cloudera;

import cn.getech.data.development.entity.JobStaticDay;
import cn.getech.data.development.entity.QueueStaticUsedHour;
import cn.getech.data.development.entity.RealtimeQueueEntity;
import cn.getech.data.development.mapper.*;
import cn.getech.data.development.model.dto.CMClusterAnalyDto;
import cn.getech.data.development.service.BigScreenService;
import cn.getech.data.development.utils.TimeUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@Component
//@ConditionalOnProperty(name = "data-development.websocket-open", havingValue = "true")
public class QueueScheduledUtils {

    @Autowired
    private BigScreenService bigScreenService;
    @Autowired
    private RealtimeQueueMapper realtimeQueueMapper;
    @Autowired
    private QueueStaticUsedHourMapper queueStaticUsedHourMapper;
    @Autowired
    private JobRunHistoryMapper jobRunHistoryMapper;
    @Autowired
    private JobStaticDayMapper jobStaticDayMapper;
    @Autowired
    private JobDepRunHistoryMapper jobDepRunHistoryMapper;
    @Autowired
    private RealtimeRunHistoryMapper realtimeRunHistoryMapper;


//    @Scheduled(cron = "0 3 0/1 * * ?")
    public void hoursQueueDataHistorys() throws Exception {
        List<RealtimeQueueEntity> realtimeQueueEntities = realtimeQueueMapper.selectList(new QueryWrapper<RealtimeQueueEntity>());
        if(CollectionUtil.isEmpty(realtimeQueueEntities)){
            return;
        }
        //查询到这个时间段的
        try {
            List<CMClusterAnalyDto> cmClusterAnalyDtos = bigScreenService.lastQueueMemory(null);
            List<QueueStaticUsedHour> queueStaticUsedHours = new ArrayList<>();
            Date now = new Date();
            Date date = new Date();
            DecimalFormat df = new DecimalFormat("0.00");
            if(CollectionUtil.isNotEmpty(cmClusterAnalyDtos)){
                for (CMClusterAnalyDto cmClusterAnalyDto : cmClusterAnalyDtos) {
                    QueueStaticUsedHour queueStaticUsedHour = new QueueStaticUsedHour();
                    queueStaticUsedHour.setQueueName(cmClusterAnalyDto.getQueueName());
                    queueStaticUsedHour.setCreateTime(now);
                    date.setTime(cmClusterAnalyDto.getX().getTime());
                    queueStaticUsedHour.setStaticTime(date);
                    queueStaticUsedHour.setStaticUsedMemoryMb(cmClusterAnalyDto.getY());
                    if(null != cmClusterAnalyDto.getY() && null != cmClusterAnalyDto.getTotalMemoryMb()){
                        queueStaticUsedHour.setStaticUsedPercent(Double.valueOf(df.format(cmClusterAnalyDto.getY()/cmClusterAnalyDto.getTotalMemoryMb() * 100)));
                    }else{
                        queueStaticUsedHour.setStaticUsedPercent(0D);
                    }
                    queueStaticUsedHours.add(queueStaticUsedHour);
                }
                //批量保存到数据库
                if(CollectionUtil.isNotEmpty(queueStaticUsedHours)){
                    queueStaticUsedHourMapper.insertBatch(queueStaticUsedHours);
                }
            }
        }catch (Exception e){
            log.error("定时统计队列的使用情况数据异常！error:{}",e.getMessage());
            throw new Exception(e);
        }
    }


//    @Scheduled(cron = "0 0/30 * * * ?")
    public void dayJobHistorys() throws Exception {
        //统计每天的任务出错个数
        Date now = new Date();
        String searchTime = TimeUtil.changeDate(now);
        //离线任务-工作流
        try {
            List<JobStaticDay> jobStaticDays = jobRunHistoryMapper.selectCountFailNum(searchTime);
            if(CollectionUtil.isNotEmpty(jobStaticDays)){
                jobStaticDayMapper.insertBatch(jobStaticDays);
            }
        }catch (Exception e){
            log.error("统计每天的离线任务-工作流出错个数异常！error:{}",e.getMessage());
        }
        //离线任务-工作流依赖
        try {
            List<JobStaticDay> jobStaticDays = jobDepRunHistoryMapper.selectCountFailNum(searchTime);
            if(CollectionUtil.isNotEmpty(jobStaticDays)){
                jobStaticDayMapper.insertBatch(jobStaticDays);
            }
        }catch (Exception e){
            log.error("统计每天的离线任务-工作流依赖出错个数异常！error:{}",e.getMessage());
        }
        //实时任务
        try {
            List<JobStaticDay> jobStaticDays = realtimeRunHistoryMapper.selectCountFailNum(searchTime);
            if(CollectionUtil.isNotEmpty(jobStaticDays)){
                jobStaticDayMapper.insertBatch(jobStaticDays);
            }
        }catch (Exception e){
            log.error("统计每天的实时任务出错个数异常！error:{}",e.getMessage());
            throw new Exception(e);
        }
    }
}
